﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Quaack.Data;
using Quaack.Web.Controls;

namespace Quaack.Web
{
    public partial class DefaultNestedMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Provider.CurrentUserPage != null)
            {
                List<Bericht> messages = Provider.CurrentUserPage.Berichten;
                FillMessageTable(messages);


                if (Provider.CurrentUser == Provider.CurrentUserPage)
                {
                    List<Profiel> users = Provider.CurrentUser.Reageerders;
                    FillProfileListView(users);
                }
            }
        }

        private void FillMessageTable(List<Bericht> berichten)
        {
            CurrentUserMessages.Rows.Clear();

            int columnCount = 0;
            TableRow rowToAdd = new TableRow();

            foreach (var bericht in berichten)
            {
                columnCount = columnCount + 1;

                var dataholder = (CombinedContentHolder)Page.LoadControl("~/Controls/CombinedContentHolder.ascx");
                dataholder.Bericht = bericht;
                dataholder.ShowContent = true;

                var cell = new TableCell();
                cell.Controls.Add(dataholder);

                rowToAdd.Cells.Add(cell);

                Table reactieTable = new Table();

                if (dataholder.Reacties.Count > 0)
                {
                    CurrentUserMessages.Rows.Add(rowToAdd);

                    TableRow reactieRowToAdd = new TableRow();

                    foreach (var reactie in dataholder.Reacties)
                    {
                        
                        var reactieHolder = (CombinedContentHolder) Page.LoadControl("~/Controls/CombinedContentHolder.ascx");
                        reactieHolder.Bericht = bericht;
                        reactieHolder.ShowContent = true;

                        var reactieCell = new TableCell();
                        reactieCell.Controls.Add(reactieHolder);

                        reactieTable.Rows.Add(reactieRowToAdd);
                    }

                   TableCell reactieTableCell = new TableCell();
                    reactieTableCell.Controls.Add(reactieTable);

                    rowToAdd.Cells.Add(reactieTableCell);

                }

                else 
                {
                    CurrentUserMessages.Rows.Add(rowToAdd);

                    rowToAdd = new TableRow();
                }
            }
        }


        private void FillProfileListView(List<Profiel> reageerders)
        {
            ProfileListView.Rows.Clear();

            int columnCount = 0;
            TableRow rowToAdd = new TableRow();

            foreach (var user in reageerders)
            {
                columnCount = columnCount + 1;

                var dataholder = (CombinedContentHolder)Page.LoadControl("~/Controls/CombinedContentHolder.ascx");
                dataholder.Profiel = user;
                dataholder.ShowContent = false;

                var cell = new TableCell();
                cell.Controls.Add(dataholder);
                rowToAdd.Cells.Add(cell);

                if (columnCount % 1 == 0 || columnCount == reageerders.Count)
                {
                    ProfileListView.Rows.Add(rowToAdd);

                    rowToAdd = new TableRow();
                }
            }
        }
    }
}